﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_WGXX_Manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateDrop();
        }
        txtADdUserID.Attributes.Add("ReadOnly", "true");
        txtAdminUserID.Attributes.Add("ReadOnly", "true");
    }

    // 绑定下拉框
    public void PopulateDrop()
    {
        drpWGXZ.PopulateFromEnum<WGXX.kindWG>();
        drpWGXZ.Items.Insert(0, new ListItem("请选择", "0"));
        drpWGLX.PopulateFromEnum<WGXX.typeWG>();
        drpWGLX.Items.Insert(0, new ListItem("请选择", "0"));
        drpWGDXLX.PopulateFromEnum<WGXX.typeUserWG>();
        drpWGDXLX.Items.Insert(0, new ListItem("请选择", "0"));

        drpAddWGXZ.PopulateFromEnum<WGXX.kindWG>();
        drpAddWGXZ.Items.Insert(0, new ListItem("请选择", "0"));
        drpAddWGLX.PopulateFromEnum<WGXX.typeWG>();
        drpAddWGLX.Items.Insert(0, new ListItem("请选择", "0"));
        drpAddWGDXLX.PopulateFromEnum<WGXX.typeUserWG>();
        drpAddWGDXLX.Items.Insert(0, new ListItem("请选择", "0"));
    }

    protected void Pager_PreRender(object sender, EventArgs e)
    {
        if (lvDate.Items.Count == 0)
            PopulateWGXX();
    }

    // 查询
    public void PopulateWGXX()
    {

        lvDate.DataSource = WGXX.GetAllByConditon(DongDong.Common.Misc.ConvertToInt(drpWGXZ.SelectedValue), DongDong.Common.Misc.ConvertToInt(drpWGLX.SelectedValue), txtAddTimeFrom.Text, txtAddTimeTo.Text, DongDong.Common.Misc.ConvertToInt(drpWGDXLX.SelectedValue), DongDong.Common.Misc.ConvertToInt(hdlAddUserID.Value), DongDong.Common.Misc.ConvertToInt(hdlAdminUserID.Value));
        lvDate.DataBind();
    }

    // 获得用户名
    public string GetUserName(object UserID)
    {
        string Return = "暂无资料";
        Client objClient = Client.Get(DongDong.Common.Misc.ConvertToInt(UserID));
        if (objClient != null)
        {
            Return = "<a target='_blank' href='../../Admin/Client/Add.aspx?UserID=" + objClient.ID + "'>" + objClient.Username + "</a>";
        }
        return Return;
    }

    protected void lvDate_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lvDate.EditIndex = -1;
        PopulateWGXX();
    }
    protected void lvDate_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        int KeyID = DongDong.Common.Misc.ConvertToInt(lvDate.DataKeys[e.ItemIndex].Value);
        WGXX.BadDelete(KeyID);
        lvDate.EditIndex = -1;
        PopulateWGXX();
    }
    protected void lvDate_PagePropertiesChanged(object sender, EventArgs e)
    {
        PopulateWGXX();
    }
    protected void lvDate_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lvDate.EditIndex = e.NewEditIndex;
        PopulateWGXX();

        // 绑定数据
        var item = lvDate.Items[lvDate.EditIndex];
        DropDownList drpEditWGXZ = (DropDownList)item.FindControl("drpEditWGXZ");
        drpEditWGXZ.PopulateFromEnum<WGXX.kindWG>();
        DropDownList drpEditWGLX = (DropDownList)item.FindControl("drpEditWGLX");
        drpEditWGLX.PopulateFromEnum<WGXX.typeWG>();
        DropDownList drpEditWGDXLX = (DropDownList)item.FindControl("drpEditWGDXLX");
        drpEditWGDXLX.PopulateFromEnum<WGXX.typeUserWG>();

        // 通过ID获得此行数据
        int KeyID = DongDong.Common.Misc.ConvertToInt(lvDate.DataKeys[e.NewEditIndex].Value);
        WGXX objWGXX = WGXX.GetByID(KeyID);

        drpEditWGXZ.SelectedValue = objWGXX.Kind.ToString();
        drpEditWGLX.SelectedValue = objWGXX.Type.ToString();
        drpEditWGDXLX.SelectedValue = objWGXX.UserType.ToString();
    }
    protected void lvDate_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        int KeyID = DongDong.Common.Misc.ConvertToInt(lvDate.DataKeys[e.ItemIndex].Value);
        var item = lvDate.Items[e.ItemIndex];

        DropDownList drpWGXZ = (DropDownList)item.FindControl("drpEditWGXZ");
        DropDownList drpWGLX = (DropDownList)item.FindControl("drpEditWGLX");
        DropDownList drpWGDXLX = (DropDownList)item.FindControl("drpEditWGDXLX");

        TextBox txtResult = (TextBox)item.FindControl("txtResult");
        if (string.IsNullOrEmpty(txtResult.Text))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "cancel1", "<script>alert('违规处理方式不能为空')</script>");
            return;
        }
        TextBox txtRemark = (TextBox)item.FindControl("txtRemark");
        if (string.IsNullOrEmpty(txtRemark.Text))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "cancel1", "<script>alert('违规说明不能为空')</script>");
            return;
        }
        WGXX objWGXX = WGXX.GetByID(KeyID);
        objWGXX.Kind = DongDong.Common.Misc.ConvertToInt(drpWGXZ.SelectedValue);
        objWGXX.Type = DongDong.Common.Misc.ConvertToInt(drpWGLX.SelectedValue);
        objWGXX.UserType = DongDong.Common.Misc.ConvertToInt(drpWGDXLX.SelectedValue);
        objWGXX.AdminUserID = DongDong.Common.Misc.ConvertToInt(AdminUser.GetCurrent().ID);
        objWGXX.Result = txtResult.Text;
        objWGXX.Remark = txtRemark.Text;
        objWGXX.Update();

        lvDate.EditIndex = -1;
        PopulateWGXX();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        PopulateWGXX();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        WGXX objWGXX = new WGXX();
        objWGXX.Kind = DongDong.Common.Misc.ConvertToInt(drpAddWGXZ.SelectedValue);
        objWGXX.Type = DongDong.Common.Misc.ConvertToInt(drpAddWGLX.SelectedValue);
        objWGXX.UserType = DongDong.Common.Misc.ConvertToInt(drpAddWGDXLX.SelectedValue);
        objWGXX.Result = txtAddResult.Text;
        objWGXX.RelationID = DongDong.Common.Misc.ConvertToInt(hdlAddUserID2.Value);
        objWGXX.Remark = txtAddRemark.Text;
        objWGXX.AdminUserID = AdminUser.GetCurrent().ID;
        objWGXX.Enable_Flag = 1;
        objWGXX.AddTime = System.DateTime.Now;
        objWGXX.Add();
        DongDong.Common.MessageBox.ShowAndRedirect(this, "添加成功", "Manage.aspx");
        PopulateWGXX();
    }
}